package cn.edu.nju.software.cdiy.action.user.all;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.cdiy.bean.Hardware;
import cn.edu.nju.software.cdiy.service.Service;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class HardwareParameterAction<T extends Hardware> extends ActionSupport implements SessionAware {

	private static String S_HARDWARE;
	private Map<String, Object> session;
	private Service<T> service;
	private int id;

	public static void setS_HARDWARE(String s_HARDWARE) {
		S_HARDWARE = s_HARDWARE;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public void setService(Service<T> service) {
		this.service = service;
	}

	public void setId(int id) {
		this.id = id;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
		T sessionHardware = (T) session.get(S_HARDWARE);
		if (sessionHardware==null || sessionHardware.getId()!=id) {
			T hardware = service.getOneById(id);
			if (hardware == null) {
				return ERROR;
			}
			session.put(S_HARDWARE, hardware);
		}
		return SUCCESS;
	}
	
}
